﻿using SHRIS.MIS.Common.Model;
using SHRIS.MIS.Common.Util.Extension;
using SHRIS.MIS.Entity.SystemManage;
using SHRIS.MIS.IService.SystemManage;
using SHRIS.MIS.Model.Param.SystemManage;
using SHRIS.MIS.Model.Result;
using SHRIS.MIS.Service.Cache;
using SHRIS.MIS.Service.SystemManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHRIS.MIS.Business.SystemManage
{

    public class MenuBLL
    {
        private IMenuService _menuService;

        private MenuCache _menuCache;

        public MenuBLL(IMenuService menuService, MenuCache menuCache)
        {
            _menuService = menuService;
            _menuCache = menuCache;
        }

        #region 获取数据
        public async Task<TEntity<List<MenuEntity>>> GetList(MenuListParam param)
        {
            var obj = new TEntity<List<MenuEntity>>();

            List<MenuEntity> list = await _menuCache.GetList();
            list = ListFilter(param, list);

            obj.Data = list;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<List<ZtreeInfo>>> GetZtreeList(MenuListParam param)
        {
            var obj = new TEntity<List<ZtreeInfo>>();
            obj.Data = new List<ZtreeInfo>();

            List<MenuEntity> list = await _menuCache.GetList();
            list = ListFilter(param, list);

            foreach (MenuEntity menu in list)
            {
                obj.Data.Add(new ZtreeInfo
                {
                    id = menu.Id,
                    pId = menu.ParentId,
                    name = menu.MenuName
                });
            }

            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<MenuEntity>> GetEntity(Guid id)
        {
            TEntity<MenuEntity> obj = new TEntity<MenuEntity>();
            obj.Data = await _menuService.GetEntity(id);
            if (obj.Data != null)
            {
                Guid parentId = obj.Data.ParentId.Value;
                if (obj.Data.ParentId.HasValue && obj.Data.ParentId.Value != Guid.Empty)
                {
                    MenuEntity parentMenu = await _menuService.GetEntity(obj.Data.ParentId.Value);
                    if (parentMenu != null)
                    {
                        obj.Data.ParentName = parentMenu.MenuName;
                    }
                }
                else
                {
                    obj.Data.ParentName = "主目录";
                }
            }
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<int>> GetMaxSort(Guid parentId)
        {
            TEntity<int> obj = new TEntity<int>();
            obj.Data = await _menuService.GetMaxSort(parentId);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TEntity<string>> SaveForm(MenuEntity entity)
        {
            TEntity<string> obj = new TEntity<string>();
            if (_menuService.ExistMenuName(entity))
            {
                obj.Message = "菜单名称已经存在！";
                return obj;
            }
            await _menuService.SaveForm(entity);

            _menuCache.Remove();

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity> DeleteForm(string ids)
        {
            TEntity obj = new TEntity();
            await _menuService.DeleteForm(ids);

            _menuCache.Remove();
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        private List<MenuEntity> ListFilter(MenuListParam param, List<MenuEntity> list)
        {
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.MenuName))
                {
                    list = list.Where(p => p.MenuName.Contains(param.MenuName) && p.IsDelete ==0).ToList();
                }
                if (param.MenuStatus > 0)
                {
                    list = list.Where(p => p.MenuStatus == param.MenuStatus && p.IsDelete == 0).ToList();
                }
            }
            return list;
        }
        #endregion
    }
}
